<?php

namespace App\Http\Controllers\H5;

use App\Library\Mall\ShopTrait;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Intervention\Image\Facades\Image;

class MyShopController extends BaseController
{
    use ShopTrait;

    public function index()
    {
        return redirect('h5/myshop/openshop');
        $shop = $this->user()->shop;
        if ($shop) {

            return $this->view('h5.myshop.index', compact('shop'));
        } else {
            return redirect('h5/myshop/openshop');
        }
    }

    public function openShop(Request $request)
    {

        $shop = $this->user()->shop;
        if ($shop)
        {
            if ($shop->auth_status)
            {
                return $this->view('h5.myshop.shop', [
                    'shop'=>$shop,
                    'auth'=>$shop->auth
                ]);
            }
        }
        return $this->view('h5.myshop.openshop');
    }

    protected function shopSaved(Request $request, $shop)
    {
        $auth = $request->input('auth');
        if ($auth)
        {
            $shop->auth->update($auth);
        }
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
     */
    public function getImage(Request $request)
    {
        $serverId = $request->input('serverId');
        $res = $this->officialAccount->media->get($serverId);

        $imagePath = 'image/'.date('Y').'/'.date('m');
        $thumbPath = 'thumb/'.date('Y').'/'.date('m');
        $filename = $res->save(storage_public_path($imagePath));

        $imagePath = $imagePath.'/'.$filename;
        $thumbPath = $thumbPath.'/'.$filename;
        $img = Image::make(storage_public_path($imagePath));

        $material = [
            'uid'=>$this->uid,
            'username'=>$this->username,
            'type'=>'image',
            'created_at'=>time(),
            'updated_at'=>time(),
            'extension'=>$img->extension,
            'size'=>$img->filesize(),
            'name'=>$filename,
            'width'=>$img->width(),
            'height'=>$img->height()
        ];

        //大图
//        if ($img->getWidth() > 1600) {
//            $scale = 1600/$img->getWidth();
//            $img->resize(1600, $img->getHeight()*$scale)->save(storage_public_path($imagePath));
//        } else {
//            $img->save(storage_public_path($imagePath));
//        }

        //缩略图
        @mkdir(dirname(storage_public_path($thumbPath)), 0777, true);
        if ($img->getWidth() > 500){
            $scale = 500/$img->getWidth();
            $img->resize(500, $img->getHeight()*$scale)->save(storage_public_path($thumbPath));
        } else {
            $img->save(storage_public_path($thumbPath));
        }

        $material['source'] = $imagePath;
        $material['thumb'] = $thumbPath;

        $image = $this->user()->materials()->create($material);
        $image->thumb = image_url($image->thumb);
        $image->image = image_url($image->source);

        return ajaxReturn(['image'=>$image]);
    }

    public function getShop()
    {
        $shop = $this->user()->shop;
        if ($shop) {
            $shop->logo = $shop->logo ? image_url($shop->logo) : '';
            $content = $shop->content;
            $auth = $shop->auth;
            $auth->id_card_pic_1 = $auth->id_card_pic_1 ? image_url($auth->id_card_pic_1) : '';
            $auth->other_pic = $auth->other_pic ? image_url($auth->other_pic) : '';

            return ajaxReturn([
                'shop'=>$shop,
                'content'=>$content,
                'auth'=>$auth
            ]);
        } else {
            return ajaxError(404, 'shop not found');
        }
    }
}
